package com.example.sandbox_basics;

import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;

public class SharedPreferencesActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shared_preferences);

		Context context = this;
		
		SharedPreferences activityPreferences = this.getPreferences(Context.MODE_PRIVATE);
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				getString(R.string.shared_preference_file_test1), Context.MODE_PRIVATE);

		String string1 = String.valueOf(activityPreferences.getLong(getString(R.string.shared_preference_file_content1), -1l));
		TextView content1 = (TextView) findViewById(R.id.text_view_content1); 
		content1.setText(string1);
		
		String string2 = String.valueOf(sharedPreferences.getLong(getString(R.string.shared_preference_file_content2), -1l));
		TextView content2 = (TextView) findViewById(R.id.text_view_content2); 
		content2.setText(string2);
		
		SharedPreferences.Editor editor = activityPreferences.edit();
		editor.putLong(getString(R.string.shared_preference_file_content1), new Date().getTime());
		editor.commit();

		System.out.println("Grabado content 1: " + activityPreferences.getLong(getString(R.string.shared_preference_file_content1), -1l));

		editor = sharedPreferences.edit();
		editor.putLong(getString(R.string.shared_preference_file_content2), new Date().getTime());
		editor.commit();

		System.out.println("Grabado content 2: " + sharedPreferences.getLong(getString(R.string.shared_preference_file_content2), -1l));

	}

}
